package com.xiaoxing.salesclient.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoxing.salesclient.di.component.DaggerYuYueChaXunComponent;
import com.xiaoxing.salesclient.di.module.YuYueChaXunModule;
import com.xiaoxing.salesclient.mvp.contract.YuYueChaXunContract;
import com.xiaoxing.salesclient.mvp.model.entity.AppointDoctor;
import com.xiaoxing.salesclient.mvp.presenter.YuYueChaXunPresenter;
import com.xiaoxing.salesclient.mvp.ui.adapter.YuYueChaXunAdapter;
import com.xiaoxing.salesclient.mvp.ui.entity.YuYueChaXun;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.BaseConstants;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;
import xiaoxing.com.salesclient.R;
import xiaoxing.com.salesclient.R2;

import static android.support.v7.widget.DividerItemDecoration.VERTICAL;
import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.SALES_CLIENT_YU_YUE_CHA_XUN_ACTIVITY)
public class YuYueChaXunActivity extends BaseActivity<YuYueChaXunPresenter> implements YuYueChaXunContract.View, OnRefreshListener {


    private static boolean mIsNeedDemo = true;
    @BindView(R2.id.empty_text)
    TextView empty_text;
    @BindView(R2.id.empty_image)
    ImageView empty_image;

    @BindView(R2.id.empty)
    View mEmptyLayout;
    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R2.id.refreshLayout)
    RefreshLayout mRefreshLayout;
    private YuYueChaXunAdapter mAdapter;
    private List<AppointDoctor.DataBean> mDataBeanList = new ArrayList<>();



    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerYuYueChaXunComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .yuYueChaXunModule(new YuYueChaXunModule(this))
                .build()
                .inject(this);

        ARouter.getInstance().inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_yu_yue_cha_xun; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, "預約查詢");

        initRefreshLayout();
        initRecyclerView();
        initEmpty();

    }

    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(this).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.autoRefresh();
        mRefreshLayout.setEnableLoadMore(false);
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(YuYueChaXunActivity.this));
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(YuYueChaXunActivity.this, VERTICAL));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new YuYueChaXunAdapter(YuYueChaXunActivity.this, mDataBeanList));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Utils.navigation(YuYueChaXunActivity.this, RouterHub.SALES_CLIENT_ZUI_XIN_XIAO_XI_XIANG_QING_ACTIVITY);
            }
        });
    }

    private void initEmpty() {
        empty_image.setImageResource(R.drawable.no_data);
        empty_text.setTextColor(getResources().getColor(R.color.public_no_data));
        empty_text.setText("暫無消息,下拉更新");
    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mPresenter.medproList(ArmsUtils.getDataTime("yyyy-MM-dd"), mSharedPreferencesHelper.getString(BaseConstants.TOKEN), mSharedPreferencesHelper.getString(BaseConstants.SHOP_ID));
        mRefreshLayout.finishRefresh();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void appointDoctorSuccessed(AppointDoctor appointDoctor) {

        if (appointDoctor != null && appointDoctor.getData() != null && appointDoctor.getData().size() > 0) {
            mEmptyLayout.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);
            mDataBeanList.clear();
            mDataBeanList.addAll(appointDoctor.getData());
            mAdapter.notifyDataSetChanged();
        } else {
            mEmptyLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        }

    }
}
